﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using MISAPP.Classify.DataAccess;
using System.Data;
using MISAPP.MISInterface;


namespace MISAPP.Classify.Service
{
    public class Service_WMG007
    {
        protected DA_WMG007 da = null;
        protected DA_WMG007 DA
        {
            get
            {
                if (da == null)
                {
                    da = new DA_WMG007();
                }
                return da;
            }
        }


        public DataTable GetSummaryData(System.Collections.ArrayList p_criteria)
        {
            string[] months = (string[])p_criteria[0];
            string[] months2 = {"",""};
            string opt = p_criteria[1].ToString();
            DataTable dt = null;
            months2[0] = months[0] + "01";
            DateTime d1 = Utility.GetLastDate(months[1]);
            string y1, m1;
            y1 = d1.Year.ToString();
            m1 = d1.Month.ToString().Length.Equals(1) 
                ? "0" + d1.Month.ToString() : d1.Month.ToString();
            months2[1] = y1 + m1 + d1.Day.ToString();

            /*
            foreach (string mon in months)
            {
                if (!Utility.IsDate(mon))
                {
                    throw new Exception("要傳入YYYYMM");
                }
            }
            
            dt = DA.GetSummaryData(getNewDates(months)
            , getAllMonths(months), opt);
            */
            dt = DA.GetSummaryData(months2
            , getAllMonths(months), opt);
            return dt;
        }

        public DataTable GetTopTenData(System.Collections.ArrayList p_criteria)
        {
            string[] months = (string[])p_criteria[0];
            string opt = p_criteria[1].ToString();
            DataTable dt = null;
            foreach (string mon in months)
            {
                if (!Utility.IsDate(mon))
                {
                    throw new Exception("要傳入YYYYMM");
                }
            }
            //dt = DA.GetTopTenData(getNewDates(months), getAllMonths(months));
            dt = DA.GetTopTenData(months, getAllMonths(months));
            return dt;
        }

        protected string[] getAllMonths(string[] p_months)
        {
            DateTime d1, d2;
            d1 = Convert.ToDateTime(string.Format("{0}-{1}-01",p_months[0].Substring(0,4)
                ,p_months[0].Substring(4) ));
            d2 = Convert.ToDateTime(string.Format("{0}-{1}-01", p_months[1].Substring(0, 4)
                            , p_months[1].Substring(4)));

            int cnt = d2.Month - d1.Month + ((d2.Year - d1.Year) * 12) + 1;
            StringBuilder sb = new StringBuilder();
            for (int i = 0; i < cnt; i++)
            {
                sb.AppendFormat("{0},", Utility.GetYYYYMM(d1.AddMonths(i)));
            }
            return sb.ToString().Split(new char[] { ',' }
                , StringSplitOptions.RemoveEmptyEntries);
        }

        protected string[] getNewDates(string[] p_months)
        {
            string d1 = string.Format("{0}{1}01", p_months[0].Substring(0, 4)
                , p_months[0].Substring(4));
            DateTime d2 = Convert.ToDateTime(string.Format("{0}-{1}-01", p_months[1].Substring(0, 4)
                            , p_months[1].Substring(4)));
            d2 = d2.AddMonths(1).AddDays(-1);
            string y, m, d;
            y = d2.Year.ToString();
            m = string.Format("0{0}", d2.Month.ToString());
            m = m.Length > 2 ? m.Substring(1) : m;
            d = string.Format("0{0}", d2.Day.ToString());
            d = d.Length > 2 ? d.Substring(1) : m;
            


            return new string[] {d1,y+m+d };

        }

    }
}
